package com.flyoil.petromp.ui.activity.activity_course;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cnpc.fyutils.EditText_util;
import com.cnpc.fyutils.ToastUtils;
import com.flyoil.petromp.R;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.entity.entity_order.MapListEntity;
import com.flyoil.petromp.mvp_p.Listener.BasePresenter;
import com.flyoil.petromp.mvp_p.SearchCertainPresenter.SearchInputCertainPresenter;
import com.flyoil.petromp.mvp_v.MessageUIView.SearchInputCertainMessageUIView;
import com.flyoil.petromp.view.EditTextDeleteView;
import com.flyoil.petromp.view.MyDatePickerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 填写搜索条件的界面
 */
public class SearchInputCourseCertainMessageUIActivity extends BaseActivity implements SearchInputCertainMessageUIView {
    private List<EditTextDeleteView> editTextDeleteViews = new ArrayList<>();
    private TextView tv_confirm, tv_starttime, tv_endtime;
    private LinearLayout line_edt, line_date;
    private SearchInputCertainPresenter presenter;
    private List<String> data_edt;

    @Override
    protected int getContentViewResId() {
        initDefaultViewsedt();
        return R.layout.activity_search_course_certain;
    }


    @Override
    protected void initViews() {
        super.initViews();
        tv_confirm = $(R.id.tv_search_input_confirm);
        tv_starttime = $(R.id.tv_search_input_certain_startdate);
        tv_endtime = $(R.id.tv_search_input_certain_enddate);
        line_edt = $(R.id.line_search_input_edt_layout);
        line_date = $(R.id.line_search_input_date_layout);
        setTextTitleName("查询");
        setTextRightName("清空");
        presenter = new SearchInputCertainPresenter(this, getIntent().getStringExtra("order_type"));
        presenter.loadUiData();
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        setBackOnclickListner(mContext);
        tv_confirm.setOnClickListener(onClickListener);
        tv_starttime.setOnClickListener(onClickListener);
        tv_endtime.setOnClickListener(onClickListener);
        setTextRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setClearEdtviewData();
            }
        });
    }


    /**
     * 清空
     */
    private void setClearEdtviewData() {
        for (int i = 0; i < editTextDeleteViews.size(); i++) {
            editTextDeleteViews.get(i).setText("");
        }
        tv_starttime.setText(getResources().getString(R.string.start_date_null_hint));
        tv_endtime.setText(getResources().getString(R.string.end_date_null_hint));
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_search_input_confirm:
                    if (getDataNull()) {
                        Intent intent = new Intent(mContext, SearchCourseCertainDataActivity.class);
                        intent.putExtra("order_type", getIntent().getStringExtra("order_type"));
                        intent.putExtra("order_status", getIntent().getIntExtra("order_status", 0));
                        intent.putStringArrayListExtra("data_edt", (ArrayList<String>) data_edt);
                        startActivity(intent);
                        finish();
                    }
                    break;
                case R.id.tv_search_input_certain_startdate://开始日期
                    show_select_date_dialog(tv_starttime);
                    break;
                case R.id.tv_search_input_certain_enddate://结束日期
                    show_select_date_dialog(tv_endtime);
                    break;
            }
        }
    };

    /**
     * 判断用户是否添加了数据
     *
     * @return
     */
    private boolean getDataNull() {
        boolean dataNull = false;
        data_edt = new ArrayList<>();
        data_edt.clear();
        for (int i = 0; i < editTextDeleteViews.size(); i++) {
            data_edt.add(editTextDeleteViews.get(i).getText().toString());
            if (editTextDeleteViews.get(i).getText().toString().length() > 0) {
                dataNull = true;
            }
        }

        if (!tv_starttime.getText().toString().equals(getResources().getString(R.string.start_date_null_hint))) {
            dataNull = true;
            data_edt.add(tv_starttime.getText().toString());
        }else{
            data_edt.add("");
        }
        if (!tv_endtime.getText().toString().equals(getResources().getString(R.string.end_date_null_hint))) {
            dataNull = true;
            data_edt.add(tv_endtime.getText().toString());
        }else{
            data_edt.add("");
        }

        if (dataNull == false) {
            ToastUtils.show_toast(getResources().getString(R.string.search_course_certain_hint));
        }
        return dataNull;
    }


    @Override
    public void updataToUI(List data) {
        if (data == null) {
            return;
        }
        for (int i = 0; i < data.size(); i++) {
            init_edt_view((MapListEntity) data.get(i));
        }
    }

    @Override
    public void showDateView(boolean showck) {
        if (showck)
            line_date.setVisibility(View.VISIBLE);
        else
            line_date.setVisibility(View.GONE);
    }

    /**
     * 将输入添加到布局中
     *
     * @param entity
     */
    private void init_edt_view(MapListEntity entity) {
        if (entity == null) {
            return;
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_search_input_certain_edt_layout, null);
        TextView tv_name = view.findViewById(R.id.tv_item_search_input_certain_name);
        EditTextDeleteView edt = view.findViewById(R.id.edt_item_search_input_certain);
        tv_name.setText(entity.getName());
        edt.setHint(entity.getValue());
        line_edt.addView(view);
        editTextDeleteViews.add(edt);
    }

    /**
     * 显示选择日期的弹框
     *
     * @param view
     */
    private void show_select_date_dialog(final TextView view) {
        EditText_util.hideKeyboard(mContext);
        final View view_backgroud = $(R.id.view_background);
        View v1 = LayoutInflater.from(this).inflate(R.layout.popup_show_select_date_dialog, null);// 创建view
        final PopupWindow popupWindow = new PopupWindow(v1, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, true);// 实例化popupwindow的方法
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        // 这个方法是为了当点击除了popupwindow的其他区域可以关闭popupwindow的方法
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        view_backgroud.setVisibility(View.VISIBLE);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                view_backgroud.setVisibility(View.GONE);
            }
        });
        final MyDatePickerView datePickerView = v1.findViewById(R.id.date_popup_show_select_date);
        datePickerView.setCancleOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        datePickerView.setConfirmOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                view.setText(datePickerView.getYear() + "-" + datePickerView.getMonth() + "-" + datePickerView.getDay());
            }
        });
    }


}
